import mongoengine as db

from app.db import BaseDocument, BaseEmbeddedDocument


class NameType(BaseEmbeddedDocument):
    zh_cn = db.StringField()
    en_us = db.StringField()


class KbVuln(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': 'kb_vuln',
        'strict': False
    }

    source = db.StringField(required=True)
    source_id = db.StringField(required=True)
    name = db.EmbeddedDocumentField(document_type=NameType, required=True)
    vuln_type = db.EmbeddedDocumentListField(document_type=NameType)
    is_plugin = db.BooleanField(default=True)
    is_deleted = db.BooleanField(default=False)
    updated_at = db.DateTimeField()
    created_at = db.DateTimeField()
    published_at = db.DateTimeField()
    severity = db.StringField(default='')
    description = db.EmbeddedDocumentField(document_type=NameType)
    cnnvd = db.ListField(default=[])
    cve = db.ListField(default=[])
    impact = db.EmbeddedDocumentField(document_type=NameType)
    owasp = db.ListField(default=[])
    recommendation = db.EmbeddedDocumentField(document_type=NameType)
    reference = db.ListField(default=[])
    zoomeye_dork = db.StringField(default='')
    zoomeye_count = db.IntField(default=0)
    event_name = db.StringField(default='')


class SystemKeywords(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': 'system_keywords',
        'strict': False
    }

    word = db.StringField(required=True, max_length=100)
    type = db.IntField(required=True, default=8, min_value=1, max_value=8)
    level = db.IntField(required=False, default=3, min_value=1, max_value=3)
    is_disabled = db.BooleanField(required=False, default=False)
    is_deleted = db.BooleanField(required=False, default=False)
    template_id = db.StringField(required=False, default='default')


class CustomKeywords(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': 'custom_keywords',
        'strict': False
    }

    word = db.StringField(required=True, max_length=100)
    type = db.IntField(required=True, default=8, min_value=1, max_value=8)
    level = db.IntField(required=False, default=3, min_value=1, max_value=3)
    is_disabled = db.BooleanField(required=False, default=False)
    is_deleted = db.BooleanField(required=False, default=False)
    custom_id = db.StringField(required=True)


class WhiteListKeywords(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': 'whitelist_keywords',
        'strict': False
    }

    word = db.StringField(required=True, max_length=100)
    custom_id = db.StringField(required=True)


class SystemProfileTemplate(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': 'system_profile_template',
        'strict': False
    }

    name = db.StringField(reqrired=True, max_length=100)
    created_at = db.DateTimeField()
    updated_at = db.DateTimeField()
    description = db.StringField(max_length=100)
    display_name = db.EmbeddedDocumentField(document_type=NameType, required=True)
    plugin_list = db.ListField(default=[])
    value = db.IntField(required=True)
    is_disabled = db.BooleanField()


class CustomProfileTemplate(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': 'custom_profile_template',
        'strict': False
    }

    name = db.StringField(reqrired=True, max_length=50)
    created_at = db.DateTimeField()
    updated_at = db.DateTimeField()
    custom_profile_template_id = db.StringField(reqrired=True)
    description = db.StringField(max_length=100)
    plugin_list = db.ListField(default=[])
    custom_id = db.StringField(reqrired=True)
    display_name = db.EmbeddedDocumentField(document_type=NameType, required=True)
    is_disabled = db.BooleanField(required=True, default=False)
    product_name = db.StringField(reqrired=True)